// 导入数据库操作模块
const { mysql } = require("../../db");
const moment = require("moment");

// 获取声优信息
// exports.Info = (req, res) => {
//     const sqlStr1 = `select * from voice_actor where is_delete = 0`;
//     const sqlStr2 = `
//         SELECT voice_actor.*,anime.animename,anime.nickname AS production FROM voice_actor
//             JOIN anime_cv ON voice_actor.id = anime_cv.cv_id
//             JOIN anime ON anime_cv.anime_id = anime.id
//             WHERE voice_actor.is_delete = 0;
//     `;

//     mysql.query(sqlStr1, (err, results1) => {
//         if (err) return res.cc(err)
//         if (results1.length === 0) return res.cc("暂无声优信息!");

//         mysql.query(sqlStr2, (err, results2) => {
//             if (err) return res.cc(err);

//             // 剔除results1中和results2中id相同项并保留results2的
//             const idSet = new Set(results2.map(item => item.id));
//             const resultsDict = {};
//             for (const item of results2) {
//                 resultsDict[item.id] = item;
//             }

//             const results = results1.filter(item => !idSet.has(item.id)).concat(Object.values(resultsDict));

//             const data = results.map(o => ({
//                 ...o,
//                 production: '《' + o.production + '》',
//                 animename: '《' + o.animename + '》',
//                 gender:
//                     o.gender === 0 ? '男' :
//                         o.gender === 1 ? '女' : '未知',
//                 country:
//                     o.country === 0 ? '国内' :
//                         o.country === 1 ? '日本' :
//                             o.country === 2 ? '欧美' : '未知',
//                 create: moment(o.create).format('YYYY-MM-DD HH:mm:ss')
//             }));

//             res.json({
//                 status: 0,
//                 message: "获取声优信息成功!",
//                 data
//             })
//         })
//     })
// }

const formatData = (results) => {
	try {
		const data = results.map(o => ({
			...o,
			production: "《" + o.production + "》",
			animename: "《" + o.animename + "》",
			gender:
				o.gender === 0 ? "男" :
					o.gender === 1 ? "女" : "未知",
			country:
				o.country === 0 ? "国内" :
					o.country === 1 ? "日本" :
						o.country === 2 ? "欧美" : "未知",
			create: moment(o.create).format("YYYY-MM-DD HH:mm:ss")
		}));
		return data;
	} catch (err) {
		throw err;
	}
};

exports.Info = async (req, res, table) => {
	try {
		const sqlStr = `SELECT * FROM ${table} WHERE is_delete = 0`;
		const [results] = await mysql.query(sqlStr);
		if (results.length === 0) return res.error("获取失败，无声优信息！");
		const data = formatData(results);
		return res.success(data);
	} catch (err) {
		return res.error(err);
	}
};

// 添加番剧
exports.Add = async (req, res, table) => {
	const { name, c_name, nickname, country, gender, intro } = req.body;
	const info = { name, c_name, nickname, country, gender, intro, creator: req.user.id };
	try {
		const sqlStr = `INSERT INTO ${table} SET ?`;
		const [results] = await mysql.query(sqlStr, [info]);
		if (results.affectedRows !== 1) return res.error("新增失败");
		return res.success("新增成功");
	} catch (err) {
		return res.error(err);
	}
};

// 修改番剧
exports.Update = async (req, res, table) => {
	const { name, c_name, nickname, gender, country, intro, id } = req.body;
	const info = [name, c_name, nickname, gender, country, intro, id];
	try {
		const sqlStr = `UPDATE ${table} SET name=?,c_name=?,nickname=?,gender=?,country=?,intro=? WHERE id = ?`;
		const [results] = await mysql.query(sqlStr, info);
		if (results.affectedRows !== 1) return res.error("修改失败");
		return res.success("修改成功");
	} catch (err) {
		return res.error(err);
	}
};

exports.Search = async (req, res, table) => {
	const info = req.params.value;

	try {
		const sqlStr = `SELECT * From ${table} WHERE (name LIKE ? OR c_name LIKE ? OR nickname LIKE ? OR intro LIKE ? OR gender LIKE ? OR country LIKE ? OR creator LIKE ?) AND is_delete = 0`; const SeletePattern = `%${info}%`;
		const values = Array(7).fill(SeletePattern);
		const [results] = await mysql.query(sqlStr, values);
		if (results.length === 0) return res.error("该声优不存在！");
		const data = formatData(results);
		return res.success(data);
	} catch (err) {
		return res.error(err);
	}
};